home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kio / kntlm.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  7.7 KB  |  234 lines

  1. /*
  2.    This file is part of the KDE libraries.
  3.    Copyright (c) 2004 Szombathelyi Gy÷rgy <gyurco@freemail.hu>
  4.   
  5.    This library is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License version 2 as published by the Free Software Foundation.
  8.  
  9.    This library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public License
  15.    along with this library; see the file COPYING.LIB.  If not, write to
  16.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17.    Boston, MA 02110-1301, USA.
  18.  */
  19.  
  20. #ifndef KNTLM_H
  21. #define KNTLM_H
  22.  
  23. #include <qglobal.h>
  24. #include <qcstring.h>
  25. #include <qstring.h>
  26.  
  27. #include <kdelibs_export.h>
  28.  
  29. /**
  30.  * @short KNTLM class implements the NTLM authentication protocol.
  31.  *
  32.  * The KNTLM class is useful for creating the authentication structures which 
  33.  * can be used for various servers which implements NTLM type authentication.
  34.  * A comprehensive description of the NTLM authentication protocol can be found
  35.  * at http://davenport.sourceforge.net/ntlm.html
  36.  * The class also contains methods to create the LanManager and NT (MD4) hashes
  37.  * of a password. 
  38.  * This class doesn't maintain any state information, so all methods are static.
  39.  */
  40.  
  41. class KIO_EXPORT KNTLM {
  42. public:
  43.  
  44.   enum Flags {
  45.     Negotiate_Unicode         = 0x00000001,
  46.     Negotiate_OEM             = 0x00000002,
  47.     Request_Target            = 0x00000004,
  48.     Negotiate_Sign            = 0x00000010,
  49.     Negotiate_Seal            = 0x00000020,
  50.     Negotiate_Datagram_Style  = 0x00000040,
  51.     Negotiate_LM_Key          = 0x00000080,
  52.     Negotiate_Netware         = 0x00000100,
  53.     Negotiate_NTLM            = 0x00000200,
  54.     Negotiate_Domain_Supplied = 0x00001000,
  55.     Negotiate_WS_Supplied     = 0x00002000,
  56.     Negotiate_Local_Call      = 0x00004000,
  57.     Negotiate_Always_Sign     = 0x00008000,
  58.     Target_Type_Domain        = 0x00010000,
  59.     Target_Type_Server        = 0x00020000,
  60.     Target_Type_Share         = 0x00040000,
  61.     Negotiate_NTLM2_Key       = 0x00080000,
  62.     Request_Init_Response     = 0x00100000,
  63.     Request_Accept_Response   = 0x00200000,
  64.     Request_NonNT_Key         = 0x00400000,
  65.     Negotiate_Target_Info     = 0x00800000,
  66.     Negotiate_128             = 0x20000000,
  67.     Negotiate_Key_Exchange    = 0x40000000,
  68.     Negotiate_56              = 0x80000000
  69.   };
  70.  
  71.   typedef struct
  72.   {
  73.     Q_UINT16 len;
  74.     Q_UINT16 maxlen;
  75.     Q_UINT32 offset;
  76.   } SecBuf;
  77.  
  78.   /**
  79.    * The NTLM Type 1 structure
  80.    */
  81.   typedef struct
  82.   {
  83.     char signature[8]; /* "NTLMSSP\0" */
  84.     Q_UINT32 msgType; /* 1 */
  85.     Q_UINT32 flags;
  86.     SecBuf domain;
  87.     SecBuf workstation;
  88.   } Negotiate;
  89.  
  90.   /**
  91.    * The NTLM Type 2 structure
  92.    */
  93.   typedef struct
  94.   {
  95.     char signature[8];
  96.     Q_UINT32 msgType; /* 2 */
  97.     SecBuf targetName;
  98.     Q_UINT32 flags;
  99.     Q_UINT8 challengeData[8];
  100.     Q_UINT32 context[2];
  101.     SecBuf targetInfo;
  102.   } Challenge;
  103.  
  104.   /**
  105.    * The NTLM Type 3 structure
  106.    */
  107.   typedef struct
  108.   {
  109.     char signature[8];
  110.     Q_UINT32 msgType; /* 3 */
  111.     SecBuf lmResponse;
  112.     SecBuf ntResponse;
  113.     SecBuf domain;
  114.     SecBuf user;
  115.     SecBuf workstation;
  116.     SecBuf sessionKey;
  117.     Q_UINT32 flags;
  118.   } Auth;
  119.   
  120.   typedef struct
  121.   {
  122.     Q_UINT32 signature;
  123.     Q_UINT32 reserved;
  124.     Q_UINT64 timestamp;
  125.     Q_UINT8  challenge[8];
  126.     Q_UINT8  unknown[4];
  127.     //Target info block - variable length
  128.   } Blob;
  129.  
  130.   /**
  131.    * Creates the initial message (type 1) which should be sent to the server.
  132.    *
  133.    * @param negotiate - a buffer where the Type 1 message will returned.
  134.    * @param domain - the domain name which should be send with the message.
  135.    * @param workstation - the workstation name which should be send with the message.
  136.    * @param flags - various flags, in most cases the defaults will good.
  137.    *
  138.    * @return true if creating the structure succeeds, false otherwise.   
  139.    */
  140.   static bool getNegotiate( QByteArray &negotiate, const QString &domain = QString::null, 
  141.     const QString &workstation = QString::null,
  142.     Q_UINT32 flags = Negotiate_Unicode | Request_Target | Negotiate_NTLM );
  143.   /**
  144.    * Creates the type 3 message which should be sent to the server after 
  145.    * the challenge (type 2) received.
  146.    *
  147.    * @param auth - a buffer where the Type 3 message will returned.
  148.    * @param challenge - the Type 2 message returned by the server.
  149.    * @param user - user's name.
  150.    * @param password - user's password.
  151.    * @param domain - the target domain. If left empty, it will be extracted 
  152.    * from the challenge.
  153.    * @param workstation - the user's workstation.
  154.    * @param forceNTLM - force the use of NTLM authentication (either v1 or v2).
  155.    * @param forceNTLMv2 - force the use of NTLMv2 or LMv2 authentication. If false, NTLMv2 
  156.    * support is autodetected from the challenge.
  157.    *
  158.    * @return true if auth filled with the Type 3 message, false if an error occured 
  159.    * (challenge data invalid, or NTLM authentication forced, but the challenge data says
  160.    * no NTLM supported).
  161.    */
  162.   static bool getAuth( QByteArray &auth, const QByteArray &challenge, const QString &user,
  163.     const QString &password, const QString &domain = QString::null, 
  164.     const QString &workstation = QString::null, bool forceNTLM = false, bool forceNTLMv2 = false );
  165.  
  166.   /**
  167.    * Returns the LanManager response from the password and the server challenge.
  168.    */
  169.   static QByteArray getLMResponse( const QString &password, const unsigned char *challenge );
  170.   /**
  171.    * Calculates the LanManager hash of the specified password.
  172.    */
  173.   static QByteArray lmHash( const QString &password );
  174.   /**
  175.    * Calculates the LanManager response from the LanManager hash and the server challenge.
  176.    */
  177.   static QByteArray lmResponse( const QByteArray &hash, const unsigned char *challenge );
  178.  
  179.   /**
  180.    * Returns the NTLM response from the password and the server challenge.
  181.    */
  182.   static QByteArray getNTLMResponse( const QString &password, const unsigned char *challenge );
  183.   /**
  184.    * Returns the NTLM hash (MD4) from the password.
  185.    */
  186.   static QByteArray ntlmHash( const QString &password );
  187.  
  188.   /**
  189.    * Calculates the NTLMv2 response.
  190.    */
  191.   static QByteArray getNTLMv2Response( const QString &target, const QString &user, 
  192.     const QString &password, const QByteArray &targetInformation, 
  193.     const unsigned char *challenge );
  194.  
  195.   /**
  196.    * Calculates the LMv2 response.
  197.    */
  198.   static QByteArray getLMv2Response( const QString &target, const QString &user,
  199.     const QString &password, const unsigned char *challenge );
  200.  
  201.   /**
  202.    * Returns the NTLMv2 hash.
  203.    */
  204.   static QByteArray ntlmv2Hash( const QString &target, const QString &user, const QString &password );
  205.  
  206.   /**
  207.    * Calculates the LMv2 response.
  208.    */
  209.   static QByteArray lmv2Response( const QByteArray &hash,
  210.     const QByteArray &clientData, const unsigned char *challenge );
  211.  
  212.   /**
  213.    * Extracts a string field from an NTLM structure.
  214.    */
  215.   static QString getString( const QByteArray &buf, const SecBuf &secbuf, bool unicode );
  216.   /**
  217.    * Extracts a byte array from an NTLM structure.
  218.    */
  219.   static QByteArray getBuf( const QByteArray &buf, const SecBuf &secbuf );
  220.  
  221.   static QByteArray createBlob( const QByteArray &targetinfo );
  222.  
  223.   static QByteArray hmacMD5( const QByteArray &data, const QByteArray &key );
  224. private:
  225.   static QByteArray QString2UnicodeLE( const QString &target );
  226.   static QString UnicodeLE2QString( const QChar* data, uint len );
  227.  
  228.   static void addBuf( QByteArray &buf, SecBuf &secbuf, QByteArray &data );
  229.   static void addString( QByteArray &buf, SecBuf &secbuf, const QString &str, bool unicode = false );
  230.   static void convertKey( unsigned char *key_56, void* ks );
  231. };
  232.  
  233. #endif /* KNTLM_H */
  234.